import re
import logging


def change_simple_if_condition_to_case(m):
    new_if_condition = "case when {}  then {} else {} end"\
        .format(str(m.group(2)).strip(), str(m.group(3)).strip(), str(m.group(4)).strip())
    return new_if_condition


def rep_simple_if(document_content):
    logging.info(" 》》》 开始执行简单 if 替换 ")

    unix_timestamp_patten = re.compile(r"(IF|if)\(([^,\(\)]*),([^,\(\)]*),([^,\(\)]*)\)")
    result = unix_timestamp_patten.sub(change_simple_if_condition_to_case, document_content)
    document_content = result
    return document_content
